package cn.softgame.crawler;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class Downloader {
	/**
	 * 根据 url 和网页类型生成需要保存的网页的文件名 去除掉 url 中非文件名字符
	 * @param url
	 * @param contentType
	 * @return
	 */
	public String getFileNameByUrl(String url, String contentType) {
		url = url.substring(7);// remove http://
		if (contentType.indexOf("html") != -1){// text/html
			url = url.replaceAll("[\\?/:*|<>\"]", "_") + ".html";
			return url;
		} else{// 如application/pdf
			return url.replaceAll("[\\?/:*|<>\"]", "_") + "."
					+ contentType.substring(contentType.lastIndexOf("/") + 1);
		}
	}

	/**
	 * 保存网页字节数组到本地文件 filePath 为要保存的文件的相对地址
	 */
	private void saveToLocal(byte[] data, String filePath) {
		try {
			File file = new File(filePath);
			if(!file.exists()){
				file.createNewFile();
			}
			DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
			for (int i = 0; i < data.length; i++)
				out.write(data[i]);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/* 下载 url 指向的网页 */
	public String download(String url) {
		String filePath = null;
		HttpClient client = new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(url);
		try {
			HttpResponse response = client.execute(httpGet);
			StatusLine statusLine = response.getStatusLine();
			int statusCode = statusLine.getStatusCode();
			// 判断访问的状态码
			if (statusCode != HttpStatus.SC_OK) {
				System.err.println("Method failed: " + statusLine);
				filePath = null;
			}

			//处理 HTTP响应内容 
			HttpEntity entity = response.getEntity();
			if (entity != null) {
                // 根据网页 url 生成保存时的文件名
    			filePath = "D:/crawler/"+ getFileNameByUrl(url,response.getFirstHeader("Content-Type").getValue());
    			saveToLocal(EntityUtils.toByteArray(entity), filePath);
		        EntityUtils.consume(entity);    
			}
		} catch (Exception e) {
			// 发生致命的异常，可能是协议不对或者返回的内容有问题
			System.out.println("Please check your provided http address!");
			e.printStackTrace();
		}finally {
			client.getConnectionManager().shutdown();
		}
		return filePath;
	}
	
	// 测试的 main 方法
	public static void main(String[] args) {
		Downloader downLoader = new Downloader();
		downLoader.download("http://www.csdn.net");
	}
}
